﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using jSLCore.Client.Presentation.AuthorizeService;

namespace jSLCore.Client.Presentation.ApplicationContainer
{
    public class jSLAuthorizationServiceBaseManager 
        : ISecurityManager
        ,IEnumDescriptionsProvider
        , IPermissionsProvider
        , IPasswordManager
    {
        private static jSLAuthorizationServiceBaseSyncedChannel _proxy;

        public jSLAuthorizationServiceBaseManager(string authorizationServiceBaseAddress)
        {
            var rem = new EndpointAddress(new Uri(authorizationServiceBaseAddress, UriKind.Absolute));
            _proxy = new jSLAuthorizationServiceBaseSyncedChannel(new BasicHttpBinding(BasicHttpSecurityMode.None) { MaxBufferSize = 2147483647, MaxReceivedMessageSize = 2147483647 }, rem);
            Authorization.Communication.InitEndPoint(rem);//allowing call to supervisor functions
        }

        public Guid Authorize(string login, string password)
        {
            return Authorization.Communication.Proxy.Authorize(login, password);
        }

        public void AuthorizeByCode(Guid code)
        {
            _proxy.AuthorizeByCode(code);
        }

        public void Logout()
        {
            _proxy.Logout();
        }

        public string GetCurrentUserName()
        {
            return _proxy.GetUserName();
        }

        public User GetCurrentUser()
        {
            return _proxy.GetCurrentUser();
        }

        public IEnumerable<EnumDescription> GetEnumDescriptions()
        {
            return _proxy.GetEnumDescriptions();
        }

        public int[] GetPermissionSet()
        {
            return _proxy.GetPermissionSet();
        }

        public void ChangePassword(string oldPwd, string newPwd)
        {
            _proxy.ChangeCurrentUserPassword(oldPwd,newPwd);
        }
    }
}